package com.wteam.dragon.system.aop.log.controller.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.system.aop.log.controller.mapper.LogErrorMapper;
import com.wteam.dragon.system.aop.log.controller.mapper.LogMapper;
import com.wteam.dragon.system.aop.log.controller.pojo.bean.Log;
import com.wteam.dragon.system.aop.log.controller.pojo.dto.LogErrorDTO;
import com.wteam.dragon.system.aop.log.controller.service.LogQueryService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

/**
 * @Author 陈晓辉
 * @Date 2020/4/1 23:53
 */
@Service
@RequiredArgsConstructor
public class LogQueryServiceImpl implements LogQueryService{
    private final LogMapper logMapper;
    private final LogErrorMapper logErrorMapper;

    @Override
    public ResultMessage pageUserLog(Page<Log> page, Log log){
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Log::getLogType, log.getLogType())
                .eq(Log::getUsername, log.getUsername())
                .orderByDesc(Log::getCreateTime);
        IPage<Log> logPage = logMapper.selectPage(page, queryWrapper);
        Dict dict = Dict.create().set("message", "获取成功")
                .set("logPage", logPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(dict);
        return resultMessage;
    }

    @Override
    public ResultMessage pageLog(Page<Log> page, Log log){
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Log::getLogType, log.getLogType())
                .orderByDesc(Log::getCreateTime)
                .like(log.getUsername() != null, Log::getUsername, log.getUsername());
        IPage<Log> logPage = logMapper.selectPage(page, queryWrapper);
        Dict dict = Dict.create().set("message", "获取成功")
                .set("logPage", logPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(dict);
        return resultMessage;
    }

    @Override
    public ResultMessage pageErrorLog(Page<LogErrorDTO> page, Log log){
        QueryWrapper<LogErrorDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("log_type", log.getLogType());
        queryWrapper.lambda().orderByDesc(LogErrorDTO::getCreateTime);
        IPage<LogErrorDTO> logPage = logErrorMapper.selectPage(page, queryWrapper);
        Dict dict = Dict.create().set("message", "获取成功")
                .set("logPage", logPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(dict);
        return resultMessage;
    }

    @Override
    public ResultMessage getErrorDetail(Long id){
        Log log = logMapper.selectById(id);
        log.setDetail(new String(log.getExceptionDetail()));
        log.setExceptionDetail(null);
        Dict dict = Dict.create().set("message", "获取成功")
                .set("log", log);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(dict);
        return resultMessage;
    }

}
